package org.jground.rx.ui.zk.report;

import java.util.Collection;
import java.util.Comparator;
import java.util.List;

import org.jground.rx.IRXObject;
import org.jground.rx.IRXObjectSynopsis;
import org.jground.rx.RXSearchCriteria;
import org.jground.rx.report.RXColumn;
import org.jground.rx.ui.IUIAttributeView;
import org.jground.rx.ui.zk.UIFrame;
import org.jground.rx.ui.zk.viewControl.UIAttributeViewControlFactory;
import org.jground.util.CollectionUtils;
import org.jground.zk.zulX.VboxX;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zul.Label;

public class UIReportColumn implements EventListener {

    private RXColumn moColumn;
    private IUIAttributeView<Component,UIFrame> uiAttributeView;
    private boolean isUp;
    private IUIReportColumnFilter uiFilterControl;

    public UIReportColumn(UIFrame uiFrame, UIReportPanel uiReportPanel, RXColumn moColumn) {
        this.moColumn = moColumn;
        uiAttributeView = UIAttributeViewControlFactory.create(uiFrame, moColumn);
        uiFilterControl = UIReportColumnFilterFactory.create(this);
    }

    public RXColumn GetRXColumn() {
        return moColumn;
    }

    public Component getValueComponent(IRXObject mod) {
        Collection<String> renderValues = renderToString(mod, true);
        VboxX vb = new VboxX();

        for (String renderValue : renderValues) {
            vb.add(new Label(renderValue));
            // StringUtils.CutAndEllipsis
        }
        return vb;
    }

    public Collection<String> renderToString(IRXObject mod, boolean cutWithMaxCols) {
        // int maxCols = moColumn.getMaxCols();
        String[] values = mod.getValues(moColumn.getAttref());
        uiAttributeView.setDataContext(mod);
        uiAttributeView.set(values);
        return uiAttributeView.renderToString();
    }

    public IUIReportColumnFilter GetFilterControl() {
        return uiFilterControl;
    }

    public void onEvent(Event he) {}

    public boolean IsUp() {
        return isUp;
    }

    public void updateSearchCriteria(RXSearchCriteria searchCriteria) {}

    public void updateFilter(List<IRXObjectSynopsis> filteredMods) {
        if (uiFilterControl != null) {
            uiFilterControl.SetFoundObjects(filteredMods);
        }
    }

    private class AscendingComparator implements Comparator<IRXObject> {

        public int compare(IRXObject o1, IRXObject o2) {
            String s1 = CollectionUtils.join(UIReportColumn.this.renderToString(o1, false));
            String s2 = CollectionUtils.join(UIReportColumn.this.renderToString(o2, false));
            return s1.compareTo(s2);
        }

    };

    private class DescendingComparator implements Comparator<IRXObject> {

        public int compare(IRXObject o1, IRXObject o2) {
            String s1 = CollectionUtils.join(UIReportColumn.this.renderToString(o1, false));
            String s2 = CollectionUtils.join(UIReportColumn.this.renderToString(o2, false));
            return s2.compareTo(s1);
        }

    };

    public Comparator<IRXObject> getAscendingComparator() {
        return new AscendingComparator();
    }

    public Comparator<IRXObject> getDescendingComparator() {
        return new DescendingComparator();
    }
}